package pool

import (
	"http_proxy/pkg/logger"
	"sync"
	"github.com/silenceper/pool"
	"time"
)

var(
	OneHttp  sync.Once
	OneTcp sync.Once
	OneUdp sync.Once
)


func CreateHttpPool()(pool.Pool,error){
	var p pool.Pool
	var e error
	OneHttp.Do(func(){
		factory := func()(interface{}, error) {
			return 1,nil
		}
		close := func(v interface{}) error {
			return nil
		}
		poolConfig := &pool.Config{
			InitialCap: 10,//资源池初始连接数
			MaxIdle:   	10,//最大空闲连接数
			MaxCap:     10,//最大并发连接数
			Factory:    factory,
			Close:      close,
			//连接最大空闲时间，超过该时间的连接 将会关闭，可避免空闲时连接EOF，自动失效的问题
			//IdleTimeout: 5 * time.Second,
		}
		p, e = pool.NewChannelPool(poolConfig)
		if e != nil {
			logger.Panic(e)
		}
	})
	return p,e
}

func CreateTcpPool()(pool.Pool,error){
	var p pool.Pool
	var e error
	OneHttp.Do(func(){
		factory := func()(interface{}, error) {
			return 1,nil
		}
		close := func(v interface{}) error {
			return nil
		}
		ping := func(v interface{}) error {
			return nil
		}
		poolConfig := &pool.Config{
			InitialCap: 20,//资源池初始连接数
			MaxIdle:   20,//最大空闲连接数
			MaxCap:     50,//最大并发连接数
			Factory:    factory,
			Close:      close,
			Ping:       ping,
			//连接最大空闲时间，超过该时间的连接 将会关闭，可避免空闲时连接EOF，自动失效的问题
			IdleTimeout: 15 * time.Second,
		}
		p, e = pool.NewChannelPool(poolConfig)
		if e != nil {
			logger.Panic(e)
		}
	})
	return p,e
}
